package org.confucianismframework.orm;

import lombok.Getter;
import lombok.SneakyThrows;
import org.confucianismframework.orm.enums.SQLType;
import org.confucianismframework.orm.pattern.EntityNamePattern;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * SQL 操作类
 *
 * @author zy
 * @deprecated
 */
public class SQL {

    /**
     * SQL 缓存
     * 已生成的 SQL 存入此 Map
     * Key 规则: className#insert
     */
    private static ConcurrentHashMap<String, StringBuilder> sqlCacheMapping = new ConcurrentHashMap<>();

    /**
     * 生成 SQL
     * @param obj 实体 {@code Entity entity = new Entity(); generator(entity, SQLType.INSERT)}
     * @param sqlType {@link SQLType}
     * @return sql
     */
    @SneakyThrows
    public static String generator(Object obj, SQLType sqlType) {

        // cache Key: className#insert
        String idCacheKey = ObjectUtils.nullSafeClassName(obj);

        Class<?> entity = obj.getClass();

        Field[] fields = entity.getDeclaredFields();

        StringBuilder sql = new StringBuilder(100);

        String tableName = EntityNamePattern.humpToLine(entity.getSimpleName());

        List<Field> fieldList = Arrays.asList(fields);

        String sqlTypeValue = sqlType.getValue();

        // 反射拼接 SQL
        if (Objects.equals(SQLType.INSERT.getValue(), sqlTypeValue)) {

            // 加载缓存
            StringBuilder cacheSql = getCacheSql(idCacheKey, sqlType);

            if (null == cacheSql) {



            } else {

                sql = cacheSql;

                System.out.println("get cache sql : type 'INSERT'");

            }

        } else if (Objects.equals(SQLType.UPDATE.getValue(), sqlTypeValue)) {

            // 加载缓存
            StringBuilder cacheSql = getCacheSql(idCacheKey, sqlType);

            if (null == cacheSql) {

            } else {

                sql = cacheSql;

                System.out.println("get cache sql : type 'UPDATE'");

            }


        } else if (Objects.equals(SQLType.DELETE.getValue(), sqlTypeValue)) {

            // 加载缓存
            StringBuilder cacheSql = getCacheSql(idCacheKey, sqlType);

            if (null == cacheSql) {



                putCacheSql(idCacheKey, sqlType, sql);

                System.out.println("get generator sql : type 'DELETE'");

            } else {

                sql = cacheSql;

                System.out.println("get cache sql : type 'DELETE'");

            }

        } else if (Objects.equals(SQLType.SELECT.getValue(), sqlType.getValue())){

            // 加载缓存
            StringBuilder cacheSql = getCacheSql(idCacheKey, sqlType);

            if (null == cacheSql) {



                putCacheSql(idCacheKey, sqlType, sql);

                System.out.println("get generator sql : type 'SELECT'");

            } else {

                sql = cacheSql;

                System.out.println("get cache sql : type 'SELECT'");

            }

        }

        return sql.toString();
    }

    /**
     * 获取 SQL 缓存
     */
    private static StringBuilder getCacheSql(String cacheKey, SQLType sqlType) {
        return sqlCacheMapping.get(cacheKey + "#" + sqlType.getValue());
    }

    /**
     * 将 SQL 加入缓存
     */
    private static void putCacheSql(String cacheKey, SQLType sqlType, StringBuilder sqlBuilder) {
        sqlCacheMapping.putIfAbsent(cacheKey + "#" + sqlType.getValue(), sqlBuilder);
    }

}
